package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.AuditRecord;
import com.rykj.qxj.server.model.entity.MetadataElement;
import com.rykj.qxj.server.model.param.AuditParams;
import com.rykj.qxj.server.model.param.AuditTableParams;
import com.rykj.qxj.server.services.IAuditManageService;
import com.rykj.qxj.server.util.AuditConstant;
import com.rykj.qxj.server.util.object.StringUtils;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Log4j
@RestController
@RequestMapping("/v1/auditManage")
@Api(tags="元数据审核")
public class AuditManageController {
    @Autowired
    private IAuditManageService auditManageService;

    @PostMapping("/saveAuditRecord")
    @ApiOperation(value="创建元数据申请信息",notes ="创建元数据申请信息")
    @EnableSystemLog(value="创建元数据申请信息",fullPath ="/v1/auditManage/saveAuditRecord")
    public Result saveAuditRecord(@RequestBody AuditRecord auditRecord){
        try{
          String errorMsg=  auditManageService.saveAuditRecord(auditRecord);
          if(StringUtils.isNotEmpty(errorMsg)){
              return new Result(false,errorMsg);
          }
          return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("创建审核失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/deleteTableAudit")
    @ApiOperation(value="删表申请",notes ="删表申请")
    @EnableSystemLog(value="删表申请",fullPath ="/v1/auditManage/deleteTableAudit")
    public Result deleteTableAudit(@RequestBody AuditTableParams auditTableParams){
        try{
            auditManageService.deleteTableAudit(auditTableParams);
            return new Result(true,"操作成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "添加失败");
        }
    }


    @PostMapping("/addAuditElements")
    @ApiOperation(value="新增申请字段信息",notes ="修改申请字段信息")
    @EnableSystemLog(value="新增申请字段信息",fullPath ="/v1/auditManage/addAuditElements")
    public Result addAuditElements(@RequestBody List<MetadataElement> MetadataElements){
        try{
            String errorMsg =  auditManageService.addAuditElements(MetadataElements);
            if(StringUtils.isNotEmpty(errorMsg)){
                return new Result(false,errorMsg);
            }
            return new Result(true,"新增成功");
        }catch (Exception e){
            log.error("新增失败", e);
            return new Result(false, "新增失败");
        }
    }

    @PostMapping("/updateAuditElements")
    @ApiOperation(value="修改申请字段信息",notes ="修改申请字段信息")
    @EnableSystemLog(value="修改申请字段信息",fullPath ="/v1/auditManage/updateAuditElements")
    public Result updateAuditElements(@RequestBody List<MetadataElement> MetadataElements){
        try{
           String errorMsg =  auditManageService.updateAuditElements(MetadataElements);
           if(StringUtils.isNotEmpty(errorMsg)){
               return new Result(false,errorMsg);
            }
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "修改失败");
        }
    }
    @GetMapping("/deleteAuditElement")
    @ApiOperation(value="删除申请字段",notes ="删除申请字段")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditElementId", value = "申请字段id",paramType = "query")
    })
    @EnableSystemLog(value="删除申请字段",fullPath ="/v1/auditManage/deleteAuditElement")
    public Result deleteAuditElement(  @RequestParam(value = "auditElementId") Integer auditElementId){
        try{
             auditManageService.deleteAuditElement(auditElementId);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除失败", e);
            return new Result(false, "删除失败");
        }
    }


    @GetMapping("/updateAuditStatus")
    @ApiOperation(value="只修改审核状态",notes ="只修改审核状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditId", value = "申请id",paramType = "query"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态,待审核auditing,审核通过pass,审核驳回reject，revok取消审核",paramType = "query")
    })
    @EnableSystemLog(value="只修改审核状态",fullPath ="/v1/auditManage/updateAuditStatus")
    public Result updateAuditStatus(    @RequestParam(value = "auditStatus",required=false) String auditStatus,
                                        @RequestParam(value = "auditId",required=false) Integer auditId){
        try{
            auditManageService.updateAuditStatus(auditId,auditStatus);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "修改失败");
        }
    }



    @GetMapping("/getAuditRecordList")
    @ApiOperation(value="分页获取审核列表",notes ="分页获取审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "applyName", value = "应用名称",paramType = "query"),
            @ApiImplicitParam(name = "applyId", value = "应用Id",paramType = "query"),
            @ApiImplicitParam(name = "tableName", value = "表名称",paramType = "query"),
            @ApiImplicitParam(name = "databaseId", value = "数据库id",paramType = "query"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态:待审核,通过,驳回",paramType = "query"),
            @ApiImplicitParam(name = "changeType", value = "变动类型: insert新增，update修改, delete删除",paramType = "query")
    })
    public Result getAuditRecordList(@RequestParam(value = "pageNum") Integer pageNum,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam(value = "applyName",required=false) String applyName,
                                     @RequestParam(value = "applyId",required=false) Integer applyId,
                                     @RequestParam(value = "tableName",required=false) String tableName,
                                     @RequestParam(value = "databaseId",required=false) Integer databaseId,
                                     @RequestParam(value = "auditStatus",required=false) String auditStatus,
                                     @RequestParam(value = "changeType",required=false) String changeType){
        try{
            PageInfo pager =  auditManageService.getAuditRecordList(pageNum,pageSize,applyName,applyId,tableName,auditStatus,changeType,databaseId);
            return new Result(pager);
        }catch (Exception e){
            log.error("分页获取审核列表失败", e);
            return new Result(false, "分页获取审核列表失败");
        }
    }

    @GetMapping("/getAuditRecordDetails")
    @ApiOperation(value="获取审核详情",notes ="获取审核详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditId", value = "审核id",paramType = "query")
    })
    public Result getAuditRecordDetails( @RequestParam(value = "auditId",required=false) Integer auditId){
        try{
            Map<String, Object> auditRecord =  auditManageService.getAuditRecordDetails(auditId);
            return new Result(auditRecord);
        }catch (Exception e){
            log.error("获取审核详情失败", e);
            return new Result(false, "获取审核详情失败");
        }
    }


    @GetMapping("/getAuditFieldDetails")
    @ApiOperation(value="获取字段申请详情列表",notes ="获取字段申请详情列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditId", value = "审核id",paramType = "query"),
            @ApiImplicitParam(name = "changeType", value = "变动类型:新增:insert,修改:update,删除:delete",paramType = "query")
    })
    public Result getAuditFieldDetails( @RequestParam(value = "auditId",required=false) Integer auditId,
                                     @RequestParam(value = "changeType",required=false) String changeType){
        try{
            List<MetadataElement> metadataElements =  new ArrayList<>();
            if(AuditConstant.CHANGETYPE_DELETE.equals(changeType)){
                metadataElements = auditManageService.getDropAuditFieldDetails(auditId);
            }else {
               metadataElements =  auditManageService.getAuditFieldDetails(auditId);
            }

            return new Result(metadataElements);
        }catch (Exception e){
            log.error("获取字段申请详情列表失败", e);
            return new Result(false, "获取字段申请详情列表失败");
        }
    }

    @PostMapping("/auditOperation")
    @ApiOperation(value="审核操作",notes ="审核操作")
    @EnableSystemLog(value="审核操作",fullPath ="/v1/auditManage/auditOperation")
    public  Result auditOperation (@RequestBody AuditParams auditParams){
        try{
            String errorMsg =  auditManageService.updateAuditOperation(auditParams);
            if(StringUtils.isNotEmpty(errorMsg)){
                return  new Result(false,errorMsg);
            }
            return  new Result(true,"审核成功");
        }catch (Exception e){
            log.error("审核失败", e);
            return new Result(false, "审核失败");
        }

    }



}
